<template>
  <div class="table-component">
    <div class="table">
      <el-table
        :data="table.data"
        :loading="table.loading"
        style="width: 100%"
        :header-cell-style="{
          background: '#F1F1F1',
          'font-size': '14px',
          'font-weight': 600,
          color: '#333333',
        }"
        :cell-style="{
          'font-size': '14px',
          'font-weight': 400,
          color: '#333333',
        }"
      >
        <el-table-column prop="dealState" label="处理状态" width="120">
          <template #default="scope">
            <p
              @click="goDelete(scope.row)"
              class="status"
              :class="
                scope.row.dealState === false ? 'incomplete' : 'completed'
              "
            >
              {{ statusToWord[scope.row.dealState] }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="businessModel" label="业务模式" width="120" />
        <el-table-column prop="desiredProduct" label="所需产品" width="160" />
        <el-table-column prop="productModel" label="产品型号" width="160" />
        <el-table-column prop="quantity" label="数量" />
        <el-table-column prop="country" label="国家" />
        <el-table-column prop="company" label="公司名称" width="120" />
        <el-table-column prop="contacts" label="用户名称" />
        <el-table-column prop="email" label="邮箱" width="160" />
        <el-table-column prop="phone" label="手机号" width="160" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createdTime" label="提交时间" width="170" />
        <el-table-column prop="dealTime" label="处理时间" width="170" />
        <template #empty>
          <el-empty :image="imgNone" />
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="pagination.pageSizes"
        layout="total,sizes, prev, pager, next, jumper"
        :total="table.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import {
  getTableInfo,
  deleteInfo,
} from "../../../service/modules/partnersInfo";
const imgNone = new URL("../../../assets/image/imgNone.png", import.meta.url)
  .href;
const props = defineProps({
  searchModel: {
    type: Object,
    default: () => {},
  },
});

const goDelete = async (row) => {
  console.log("row ===>", row);
  if (row.dealState) {
    return;
  } else {
    const res = await deleteInfo({
      id: row.id,
      opt: 1,
    });
    console.log("res ===>", res);
    if (res.data.code === 0) {
      getData();
      ElMessage({
        type: "success",
        message: "已完成1条待办",
      });
    }
  }
};

const searchModel = ref({
  date: ["", ""],
  status: "",
});

watch(
  () => props.searchModel,
  (value) => {
    searchModel.value = value;
    getData();
    console.log(searchModel.value);
  },
  {
    deep: true,
  }
);

const table = reactive({
  data: [],
  total: 0,
  loading: true,
});

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  pageSizes: [10, 20, 30, 50],
});

const statusToWord = {
  true: "已完成",
  false: "完成待办",
};

const getData = async () => {
  const res = await getTableInfo({
    dealState: searchModel.value.status,
    begin: searchModel.value.date[0],
    end: searchModel.value.date[1],
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize,
  });
  if (res.data.code === 0) {
    table.data = res.data.result.records;
    table.total = res.data.result.total;
    table.loading = false;
  }
};
const handleCurrentChange = (value) => {
  pagination.currentPage = value;
  getData();
};

const handleSizeChange = (value) => {
  pagination.pageSize = value;
  getData();
};

getData();
</script>

<style lang="less" scoped>
.table-component {
  .table {
    margin-bottom: 15px;
    .status {
      width: 79px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .completed {
      background: #efefef;
      border-radius: 8px;
      border: 1px solid #666666;
    }
    .incomplete {
      cursor: pointer;
      background: #ffffff;
      border-radius: 8px;
      border: 1px solid var(--el-color-primary);
      color: var(--el-color-primary);
    }
  }
  .pagination {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}
</style>
